'use strict';

const { Service } = require('egg');
class AdminService extends Service {
  async createUser (info) {
    const { ctx, app } = this;
    try {
      const result = await app.mysql.insert('admin', info); // 给list表新增一条数据
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 审核求助信息
  async editHelp (id, isSolve, isEamine) {
    const { ctx, app } = this;
    try {
      const result = await app.mysql.update('needDrug', { isSolve, isEamine }, {
        where: {
          id
        }
      });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 审核帮助信息
  async examineProHelp (id, isSolve, isEamine) {
    const { ctx, app } = this;
    try {
      const result = await app.mysql.update('provideDrug', { isSolve, isEamine }, {
        where: {
          id
        }
      });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 删除求助信息
  async delHelp (id) {
    const { app } = this;
    try {
      const result = app.mysql.delete('needDrug', {
        id,
      });
    } catch (error) {
      return null;
    }
  }
  //删除帮助
  async delProHelp (id) {
    const { app } = this;
    try {
      const result = app.mysql.delete('provideDrug', {
        id,
      });
    } catch (error) {
      return null;
    }
  }
  // 更新后台用户
  async editAdminUser (id, account, password, role) {
    const { ctx, app } = this;
    try {
      const result = await app.mysql.update('admin', { account, password, role }, {
        where: {
          id
        }
      });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 更新用户
  async editUser (id, account, password, phone, role) {
    const { ctx, app } = this;
    try {
      const result = await app.mysql.update('user', { account, password, phone, role }, {
        where: {
          id
        }
      });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 删除用户
  async deleteUser (id) {
    const { app } = this;
    try {
      const result = app.mysql.delete('user', {
        id,
      });
    } catch (error) {
      return null;
    }
  }
  //删除后台用户
  async deleteAdminUser (id) {
    const { app } = this;
    try {
      const result = app.mysql.delete('admin', {
        id,
      });
    } catch (error) {
      return null;
    }
  }
}
module.exports = AdminService;
